package com.csun.dsms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgIndustryElectricityDataMapper;
import com.csun.dsms.model.huang.HgIndustryElectricityData;
import com.csun.dsms.service.HgIndustryElectricityDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HgIndustryElectricityDataServiceImpl implements HgIndustryElectricityDataService {

    @Resource
    private HgIndustryElectricityDataMapper hgIndustryElectricityDataMapper;

    @Override
    public Wrapper importIndustryElectricityData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgIndustryElectricityData hgIndustryElectricityData = new HgIndustryElectricityData();
                hgIndustryElectricityData.setStatisticalYear(Convert.toInt(excelMap.get("statistical_year")));
                hgIndustryElectricityData.setStatisticalMonth(Convert.toInt(excelMap.get("statistical_month")));
                hgIndustryElectricityData.setStatisticalQuarter(Convert.toInt(excelMap.get("statistical_quarter")));
                hgIndustryElectricityData.setStatisticalTimeType(Convert.toInt(excelMap.get("statistical_time_type")));
                hgIndustryElectricityData.setArea(Convert.toStr(excelMap.get("area")));
                hgIndustryElectricityData.setAreaLevel(Convert.toInt(excelMap.get("area_level")));
                hgIndustryElectricityData.setProvincialRank(Convert.toInt(excelMap.get("provincial_rank")));
                hgIndustryElectricityData.setMunicipalRank(Convert.toInt(excelMap.get("municipal_rank")));
                hgIndustryElectricityData.setPowerCount(Convert.toDouble(excelMap.get("power_count")));
                hgIndustryElectricityData.setPowerGrowthCount(Convert.toDouble(excelMap.get("power_growth_count")));
                hgIndustryElectricityData.setPowerGrowthSpeed(Convert.toDouble(excelMap.get("power_growth_speed")));
                hgIndustryElectricityData.setEntryDepartment(Convert.toStr(excelMap.get("entry_department")));
                hgIndustryElectricityData.setEntryPerson(Convert.toStr(excelMap.get("entry_person")));
                hgIndustryElectricityData.setIsApproved(Convert.toInt(excelMap.get("is_approved")));
                hgIndustryElectricityData.setCreatedTime(DateUtil.parse(Convert.toStr(excelMap.get("created_time")), DatePattern.NORM_DATETIME_PATTERN));
                hgIndustryElectricityDataMapper.insert(hgIndustryElectricityData);

            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入数据成功");
        } catch (Exception e) {
            log.error("导入数据报错", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
